<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 15:49
 */

namespace app\admin\controller;


use app\Model\UserLeverModel;
use app\Model\UsersModel;
use think\Collection;
//会员等级
class Memberlv extends Common
{
    /*
     * 会员等级页面
     */
    public function index()
    {
        $getuserlv = UserLeverModel::order('amount','desc')->paginate();//列出所有会员等级
        $data['data'] = $getuserlv;
        $this->getConAndAct('member');
        return view('index',$data);
    }
    /*
     * 会员等级添加/编辑页面
     */
    public function add()
    {
        $getData = input('get.');
        if ($getData){
            $getUser = UserLeverModel::find($getData['level_id']);
            $data['data']=$getUser;
//            dump($getUser->toArray());die();
            return view('edit',$data);//编辑页

        }
        return view('add');
    }
    /*
     * 会员等级添加
     */
    public function addData()
    {
//        dump(input('post.'));die();
        $postData = input('post.');
        if (isset($postData['level_id'])){
            sole('tp_user_level',['level_name'=>$postData['level_name']],['level_id',$postData['level_id']],"会员等级");//判断修改账号是否存在
            $setUser = UserLeverModel::update($postData,['level_id'=>$postData['level_id']]);

        }else{
            sole('tp_user_level',['level_name'=>$postData['level_name']],[],'会员等级');//判断修改账号是否存在
            $setUser = UserLeverModel::insert($postData);
        }
        return setDatabase($setUser);
    }
    /*
    * 删除会员等级
    */
    public function del(){
        $postData = input('post.');
        //注册会员不可删除
        if ($postData['id']==1){
            return retJson('',201,'注册会员不可删除');
        }
        //有用户为该等级时不可删除
        $getUserlv=UsersModel::where('level',$postData['id'])->find();
        if ($getUserlv){
            return retJson('',201,'该等级存在会员');
        }
        $delUser = UserLeverModel::destroy($postData['id']);
        return setDatabase($delUser);
    }
}